/**
 * 类名：TaskService
 * 创建日期： 2014-11-16 下午6:36:15
 * 
 */
package com.erms.domain;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import com.erms.bean.Homework;
import com.erms.bean.Task;
import com.erms.bean.Teach;
import com.erms.dao.HomeworkDAO;
import com.erms.dao.TaskDAO;
import com.erms.util.TimeUtil;

/**
 * @author hmb
 *         备注：
 * 
 */
public class TaskService extends BaseService {
	@Resource
	private TaskDAO			taskDAO;
	@Resource
	private HomeworkDAO		homeworkDAO;
	@Resource
	private HomeworkService	homeworkService;
	
	private Task task;

	public Task getTask() {

		return task;
	}

	public boolean save(Task task, File srcFile, String srcFileName,
			String srcContentType, String newFileName, String description,
			String userID) {

		String teachID = task.getTeach().getId();
		return save(teachID, task.getTitle(), task.getContent(),
				task.getDeadline(), srcFile, srcFileName, srcContentType,
				newFileName, description, userID);

	}

	/**
	 * 保存布置的作业，带附件
	 * 
	 * @param teachID 开课信息id
	 * @param title 作业标题
	 * @param content 作业内容
	 * @param deadline 作业截止时间戳
	 * @param srcFile 文件（以下三个为struts2文件上传必须涉及的三个相关变量）
	 * @param srcFileName 文件名
	 * @param srcContentType 文件类型
	 * @param newFileName 新文件名，如果需要重命名则null
	 * @param description 文件描述
	 * @param userID 文件上传者id
	 * @return 操作结果
	 */
	public boolean save(String teachID, String title, String content,
			Integer deadline, File srcFile, String srcFileName,
			String srcContentType, String newFileName, String description,
			String userID) {

		logger.debug("保存作业任务：teachid=" + teachID + ",title=" + title);
		boolean result = true;
		if (srcFile != null) {
			result = fileService.save(srcFile, srcFileName, srcContentType,
					newFileName, description,
					FileService.TYPE_TASK_ATTACH_FILE, userID);
		}
		try {

			if (result) {
				task = new Task();
				Teach teach = new Teach();
				teach.setId(teachID);
				fileInfo = fileService.getFileInfo();
				task.setTeach(teach);
				task.setDeadline(deadline);
				task.setTitle(title);
				task.setContent(content);
				task.setFileInfo(fileInfo);
				task.setTime(TimeUtil.getTimeStamp());

				taskDAO.save(task);
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.debug("保存作业失败", e);
			result = false;
		}
		return result;

	}

	/**
	 * 保存布置的作业，不带附件
	 * 
	 * @param teachID 开课信息id
	 * @param title 作业标题
	 * @param content 作业内容
	 * @param deadline 截止时间戳
	 * @return 操作结果
	 */
	public boolean save(String teachID, String title, String content,
			Integer deadline) {

		return save(teachID, title, content, deadline, null, null, null, null,
				null, null);
	}

	/**
	 * 删除作业任务
	 * 
	 * @param id 任务ID
	 * @return
	 */
	public boolean delete(Integer id) {

		logger.debug("删除作业任务：id=" + id);
		boolean result = true;
		try {
			Task task = taskDAO.findById(id);
			fileInfo = task.getFileInfo();
			@SuppressWarnings("unchecked")
			List<Homework> homeworks = homeworkDAO
					.findByProperty("task.id", id);
			int n = homeworks.size();
			logger.debug("删除本次任务下的所有的作业,总数：" + n);
			for (int i = 0; i < n; i++) {
				Homework homework = homeworks.get(i);
				homeworkService.delete(homework.getId());
			}
			taskDAO.delete(task);
			if (fileInfo == null) {
				logger.debug("没有附件");
			} else {
				logger.debug("找到附件：id=" + fileInfo.getId());
				fileService.delete(fileInfo.getId());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.debug("删除作业任务失败");
			result = false;
		}

		return result;

	}

	public Task find(Integer id) {

		return taskDAO.findById(id);
	}
}
